﻿using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace IOCtrls
{
    [DefaultEvent("StateChanged")]
    public partial class OutputCtr : UserControl
    {
        private string _text;
        private bool _state;

        public OutputCtr()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);

            SetStyle(
                ControlStyles.OptimizedDoubleBuffer
                | ControlStyles.ResizeRedraw
                | ControlStyles.Selectable
                | ControlStyles.AllPaintingInWmPaint
                | ControlStyles.UserPaint
                | ControlStyles.SupportsTransparentBackColor,
                true);

        }

        protected override void OnSizeChanged(EventArgs e)
        {
            ResizeSwitch();
            base.OnSizeChanged(e);
        }

        protected override void OnResize(EventArgs e)
        {
            ResizeSwitch();
            base.OnResize(e);
        }

        private void ResizeSwitch()
        {
            uiSwitch1.Height = Height - 4;
            uiSwitch1.Width = uiSwitch1.Height * 2;
        }

        [Browsable(true), Category("自定义属性")]
        public override string Text
        {
            get => _text;
            set
            {
                _text = value;
                uiLabel1.Text = _text;
            }
        }

        [Description("True 为On False 为Off"), Category("自定义属性")]
        public bool State
        {
            get
            {
                return _state;
            }
            set
            {
                _state = value;
                uiSwitch1.Active = value;
            }
        }

        public event Action<object, bool> StateChanged;

    

        private void uiSwitch1_Click(object sender, EventArgs e)
        {
            State = !State;
            StateChanged?.Invoke(this.Tag,State);
        }

        public Action<object> LabelDoubleClick;

        private void uiLabel1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            LabelDoubleClick?.Invoke(this);
        }

    }
}
